package home

import (
	v1 "go_muban/api/home/v1"
	"go_muban/internal/service"
	"go_muban/utility"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

// Controller 首页控制器
type Controller struct{}

// NewController 创建控制器
func NewController() *Controller {
	return &Controller{}
}

// Index 首页
func (c *Controller) Index(r *ghttp.Request) {
	ctx := r.GetCtx()

	// 获取首页数据
	req := &v1.IndexReq{}
	res, err := service.Home().GetIndex(ctx, req)
	if err != nil {
		g.Log().Error(ctx, "获取模板分类失败:", err)
	}

	categories, err := service.Home().GetCategories(ctx, 6)
	if err != nil {
		g.Log().Error(ctx, "获取模板分类失败:", err)
	}

	utility.WriteTplByLayout(r, "index.html", g.Map{
		"title":             "首页",
		"featuredTemplates": res.FeaturedTemplates,
		"categories":        categories,
	})

}
